package cc.higou.service;

import cc.higou.core.pojo.Brand;
import cc.higou.core.tools.Constants;
import cc.higou.core.tools.Encoding;
import cc.higou.core.tools.PageHelper;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：品牌Brand
 *
 * @author:dongxing
 * @time:--
 */
@Controller
public class BrandController {
    @Autowired
    private BrandService brandService;

    //通用的brand跳转
    @RequestMapping(value = "/console/brand/{pageName}.do")
    public String centerBrandShow(@PathVariable("pageName") String pageName, Model model) {

        return "brand/" + pageName;
    }

    //查询brand的list页面的跳转
    @RequestMapping(value = "/console/brand/list.do")
    public String centerBrandShow(Integer pageNum, Integer pageSize, Model model, String name, Integer isDisplay) {

        //处理get请求的乱码问题
        name = Encoding.encodeGetRequest(name);

        //将数据封装进brand对象中
        Brand brand = new Brand();
        brand.setName(name);
        brand.setIsDisplay(isDisplay);

        PageHelper.Page<Brand> page = brandService.findByExample(pageNum, pageSize, brand);

        //将查询数据传递给前台
        model.addAttribute("page", page);
        //回显条件查询的name
        model.addAttribute("name", name);
        //回显
        model.addAttribute("isDisplay", isDisplay);

        return "brand/list";
    }

    //修改页面数据的页面回显
    @RequestMapping(value = "/console/brand/showEdit.do")
    public String centerBrandShowEdit(@RequestParam("brandId") Long id, Model model) {

        Brand brand = brandService.findById(id);
        model.addAttribute("brand", brand);
        return "brand/edit";
    }

    //保存修改的页面数据
    @RequestMapping(value = "/console/brand/doEdit.do")
    public String centerDoEditShow(Brand brand) {

        brandService.update(brand);
        return "redirect:list.do";
    }

    //添加商品
    @RequestMapping(value = "/console/brand/doAdd.do")
    public String centerDoAddShow(Brand brand) {
        brandService.add(brand);
        return "redirect:list.do";

    }

    //删除文件
    @RequestMapping(value = "/console/brand/delete.do")
    public String centerDeleteShow(String ids, Brand brand, Integer pageNum) throws UnsupportedEncodingException {

        //进行编码，将其变成iso-8859-1,传递给list.do
        String name = new String(brand.getName().getBytes("UTF-8"), "ISO8859-1");
        brand.setName(name);
        brandService.deleteByids(ids);
        String n = brand.getName() == null ? "" : brand.getName();
        String i = brand.getIsDisplay() == null ? "" : brand.getIsDisplay() + "";

        return "redirect:list.do?name=" + n + "&isDisplay=" + i + "&pageNum=" + pageNum;


    }

    @RequestMapping(value = "/console/brand/query.do")
    @ResponseBody
    public List<Brand> queryBrands() {
        return brandService.findAll();
    }

    //上传文件
    @RequestMapping(value = "/uploadFile.do")
    @ResponseBody
    public Map<String, String> uploadFile(MultipartFile mpf) {
        System.out.println(mpf.getOriginalFilename());

        try {
            //读取配置文件，使用的是Spring带的
            ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
            //resource.getClassLoader()获取到WEB-INF下的classes文件夹路径
            URL url = resource.getClassLoader().getResource("fdfs_client.conf");

            //加载数据(需要的是String类型的数据，故使用url.getPath转换)
            ClientGlobal.init(url.getPath());
            //创建老大的客户端
            TrackerClient trackerClient = new TrackerClient();
            //获取老大的服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取小弟的客户端
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);

            //获取文件的后缀名
            String extension = FilenameUtils.getExtension(mpf.getOriginalFilename());
            // 将文件上传到分布式文件系统，并返回文件的存储路径及名称
            String file1 = storageClient1.upload_file1(mpf.getBytes(), extension, null);
            System.out.println(file1);
            // 返回json格式的字符串（图片的绝对网络存放地址)

            HashMap<String, String> map = new HashMap<>();
            map.put("path", Constants.FDFS_SERVER + file1);
            return map;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

}
